package com.fresher.aspect;

import java.util.Locale;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import com.fresher.service.UserService;
import com.fresher.service.LogService;
@Component
@Aspect
public class LoggingAspect {
	@Autowired
	private MessageSource messageSource;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private LogService logService;
	
	@Before("execution(* com.fresher.service.AccountService.getListAccount(..))")
	public void logBeforeAcc(JoinPoint joinPoint) {
		System.out.println("=======================");
		System.out.println("View accounts");
		System.out.println("method called : " + joinPoint.getSignature().getName());
		System.out.println("******");
	}
	
	@Before("execution(* com.fresher.service.TransactionService.getListTransaction(..))")
	public void logBeforeTrans(JoinPoint joinPoint){
		System.out.println("=======================");
		System.out.println("View transactions");
		System.out.println("method called : " + joinPoint.getSignature().getName());
		System.out.println("******");
	}
	
	@AfterReturning("execution(* com.fresher.authentication.MyAuthenticationSuccessHandler.onAuthenticationSuccess(..))")
	public void logLoginDetail() {
		logService.addLog("User logged in: " + logService.getCurrentDateTime(), userService.getLoggedUserName());
	}
	
	@AfterReturning("execution(* com.fresher.authentication.MyLogoutSuccessHandler.onLogoutSuccess(..))")
	public void logLogoutDetail(JoinPoint joinPoint) {
		Object args[] = joinPoint.getArgs();
		Authentication auth = (Authentication)args[2];
		String username="";
		try {
			username = auth.getName();
		} catch(Exception ex) {
			
		}
		logService.addLog("User logged out: " + logService.getCurrentDateTime(), username);
	}
	
	@AfterReturning(pointcut="execution (* com.fresher.service.TransactionService.createTransactionService(..))", returning= "result")
	public void logTransactionDetail(JoinPoint joinPoint, Object result) {
		/*if(result.equals("Tranfer Succsessful!") || result.equals("Chuyển khoản thành công!"))*/
		Locale locale = LocaleContextHolder.getLocale();
		if(result.equals(messageSource.getMessage("label.transSuccess", null, locale).toString())){
		Object args[] = joinPoint.getArgs();
		String details = "Transaction created: [From: " + args[0] + "] [To: " + args[1] 
				+ "] [Amount: " + args[2] + "] [Info: " + args[3] + "] [Date: " + args[4]+"]";

		logService.addLog(details, userService.getLoggedUserName());
		System.out.println(details);
		}
	}
	
	@AfterReturning("execution (* com.fresher.service.TransactionService.getListTransaction(..))")
	public void logViewTransactionDetail() {
		logService.addLog("User do view transaction: " + logService.getCurrentDateTime(), userService.getLoggedUserName());
	}
}